#ifndef MYSLIDER_H
#define MYSLIDER_H

#include <QMouseEvent>
#include <QCoreApplication>
#include <QSlider>
#include <QLabel>
#include <QTimer>
#include <QPropertyAnimation>
#include <QDebug>

// -------------------------- 自定义事件 --------------------------------
class SliderClickedEvent : public QEvent
{
public:
    SliderClickedEvent(QString valueString = "");
    ~SliderClickedEvent();

    static Type eventType();
    QString getValueString(void);

private:
    static Type m_EventType;
    QString m_String;
};

//支持拖拽和点击的slider
class MySlider : public QSlider
{
    Q_OBJECT
public:
    MySlider(QWidget *parent = 0) : QSlider(Qt::Horizontal, parent)
    {
    }
    ~MySlider()
    {

    }
protected:
    void mousePressEvent(QMouseEvent *ev)
    {
        //注意应先调用父类的鼠标点击处理事件，这样可以不影响拖动的情况
        QSlider::mousePressEvent(ev);

        //获取鼠标的位置，这里并不能直接从ev中取值（因为如果是拖动的话，鼠标开始点击的位置没有意义了）
        double pos = ev->pos().x() / (double)width();
        double value = pos * (maximum() - minimum()) + minimum();
        //value + 0.5 四舍五入
        setValue(value + 0.5);
        //向父窗口发送自定义事件event type，这样就可以在父窗口中捕获这个事件进行处理

        SliderClickedEvent *sendSliderClickedEvent = new SliderClickedEvent("");
        //qDebug() << nativeParentWidget()->objectName();
        bool result = QCoreApplication::sendEvent(nativeParentWidget(), sendSliderClickedEvent);
        //qDebug() << "The Dispose Result Is " << result;
        delete sendSliderClickedEvent;
    }
};

//
class MyLabel : public QLabel
{
    Q_OBJECT
public:
    MyLabel(QWidget *parent = 0) : QLabel(parent)
    {
        //  设置背景
//        setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
//        setAttribute(Qt::WA_TranslucentBackground);
//        setAttribute(Qt::WA_QuitOnClose);
//        setAttribute(Qt::WA_DeleteOnClose);


    }
    ~MyLabel()
    {
        //qDebug() << "label close";
    }

    void setText(QString text)
    {
        QLabel::setText(text);
        show();
        //QTimer::singleShot(200, this, SLOT(closeAnimation()));
        closeAnimation();
    }

private slots:
    void closeAnimation() {
        QPropertyAnimation *animation =
        new QPropertyAnimation(this, "windowOpacity", this);
        animation->setDuration(1000);
        animation->setStartValue(1);
        animation->setEndValue(0);
        animation->start();
        connect(animation, SIGNAL(finished()), SLOT(hide()));
    }
};

#endif // MYSLIDER_H
